package nz.ac.massey.spidernetpn.database;

import java.io.FileReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.events.Attribute;
 
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
 
 
public class GraphLoadXML {
	private int edgeCounter = 0;
	private int nodeCounter =0;
	private String workingDirectory;
	private DirectedGraph<URL, Integer> graph = new DirectedSparseMultigraph<URL, Integer>();
	public GraphLoadXML(String workingDirectory) {
		// TODO Auto-generated constructor stub
		this.workingDirectory = workingDirectory;
		System.out.println("grDB init with this dir "+workingDirectory);
	}
	public DirectedGraph<URL, Integer> loadGraph(String filename) {
	
	
	try {
 
      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setValidating(false);
      
     
      
      SAXParser saxParser = factory.newSAXParser();
 
      DefaultHandler handler = new DefaultHandler() {
  
        public void startElement(String uri, String localName,
            String qName, Attributes attributes)
            throws SAXException {
 
        	
         //System.out.println("ORinting uri:" + uri +" ln "+ localName+" qname "+ qName +" at "+ attributes);
          if(qName.matches("edge")){
        	  
        	  try {
        		  URL edgeStart = null;
        		  URL edgeEnd = null;
        	  
		          for(int i=0;i<attributes.getLength();i++){
		        	 // System.out.println("Attrib "+i+" "+attributes.getValue(i));
					
		        	  if(i==0){
		        		  edgeStart = new URL(attributes.getValue(i));
		        	  }
		        	  if(i==1){
		        		  edgeEnd = new URL(attributes.getValue(i));  
		        	  }
					
						
					}
		          graph.addEdge(edgeCounter, edgeStart, edgeEnd);
		          edgeCounter++;
		          
        	  } catch (Exception e) {
        		// TODO Auto-generated catch block
					e.printStackTrace();
	          }
        
        	  System.out.println("Edges Loaded "+edgeCounter);
        	  
          }
          
          if(qName.matches("node")){  
        	  try {
	        		String atribURL = attributes.getValue(0);
		        	//System.out.println("Attrib "+atribURL);
		        	URL newURL = null;
					
					newURL = new URL(atribURL);
					graph.addVertex(newURL);
						
					nodeCounter++;
					
					System.out.println("Nodes Loaded "+nodeCounter);
		          
	          } catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
			  }
	          
          }
          
          
 
        }
 
        public void endElement(String uri, String localName,
                String qName)
                throws SAXException {

        }
 
        public void characters(char ch[], int start, int length)
            throws SAXException {
         
        }
 
      };
 
     // String fname = System.getProperty("user.dir");
     // String fname = "H:\\Software C Workspace\\spidernetpn\\test\\Graphs\\"+filename;
    //  fname.replaceAll("\\","/");
      
      String  fname = workingDirectory+filename;
      
      System.out.println("trying to load "+fname);
      InputSource is = new InputSource(new FileReader(fname));

     // saxParser.setProperty(name, value)
     // saxParser.setProperty("Validate", false);
     // saxParser.isValidating();
      saxParser.parse(is, handler);
     
    } catch (Exception e) {
      e.printStackTrace();
    }
    if(graph==null){
    	System.out.println("Loaded graph is null");
    }
    return graph;
  }
 
}
